new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Dingtalk_Message_SerialNumber: '',
                Dingtalk_Message_Name: '',
                Template_SerialNumber: '',
                Msg_Param: '',
                App_ID: '',
                Enterprise_Info_SerialNumber: $.cookie('enterprise'),
                Children: {
                    dingtalk_message_userids: []
                }
            },
            rules: {
                Dingtalk_Message_Name: [{ required: true, message: '请输入消息推送实例名称', trigger: 'blur' }],
                Template_SerialNumber:[{ required: true, message: '请选择消息推送模板', trigger: 'change' }]
            },
            template: [],//复合模版
            userids: [],//推送人员
            loading: false,
            Msg_Key: '',//模板类型
            Msg_Param: {},//模板预览
            User_Id: [],
        }
    },
    mounted() {
        this.form.App_ID = sessionStorage.getItem("App_ID");
        this.getData();
    },
    methods: {
        getData() {
            const that = this;
            $.ajax({
                type: 'POST',
                url: DomainName + "/getInquiryAction?token=" + $.cookie('token'),
                data: {
                    Data: "dingtalk_message_template",
                },
                success: function (data) {
                    if (data.length > 0) {
                        that.template = data;
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
            $.ajax({
                type: 'POST',
                url: DomainName + "/getInquiryAction?token=" + $.cookie('token'),
                data: {
                    Data: "dingtalk_userids_info",
                },
                success: function (data) {
                    if (data.length > 0) {
                        that.userids = data;
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        change(e) {//选择模板
            if (e) {
                let obj = this.template.filter(i => i.Template_SerialNumber == e)[0];
                this.Msg_Key = obj.Msg_Key
                this.Msg_Param = obj.Msg_Param;
                console.log(obj.Msg_Param);
            } else {
                this.Msg_Key = '';
                this.Msg_Param = {};
            }
        },
        submitForm(formName) {
            let _this = this
            if (this.User_Id.length == 0) {
                this.$message.error("发送成员不能为空")
                return
            }
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true
                    _this.form.Msg_Param = JSON.stringify(_this.Msg_Param);
                    _this.User_Id.forEach(User_ID => {
                        _this.form.Children.dingtalk_message_userids.push({
                            Message_Userids_SerialNumber: '',
                            Dingtalk_Message_SerialNumber: '',
                            User_ID
                        })
                    })
                    $.ajax({
                        url: DomainName + '/getAddAction?token=' + $.cookie('token'),
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({
                                dingtalk_message_config: [_this.form]
                            }),
                            Operation_Name: '添加消息推送实例'
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                _this.$message.success("提交成功")
                                window.history.go(-1)
                            } else {
                                _this.$message.error("提交失败，请稍后再试")
                            }
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})